<?php

    class IndexController extends Zend_Controller_Action
    {

        public function init()
        {
            /* Initialize action controller here */
        }

        public function indexAction()
        {
            $model = new Application_Model_Common();
            $custom_model = new Application_Model_Custom();
            $page = $this->_getParam("page", 1);
            $all_event_where_cond = "event_active = '1'";
            $all_event_order_by = "event_id DESC";
            $sort_tag_class = "distance";

//            prd($this->getParam("sort_type"));

            if ($this->getParam("sort_type"))
            {
                $sort_type = trim($this->getParam("sort_type"));

                switch ($sort_type)
                {
                    case "date":
                        {
                            $all_event_order_by = "event_id DESC";
                            $sort_tag_class = "date";
                            break;
                        }
                    case "groupsize":
                        {
                            $all_event_order_by = "event_group_size DESC";
                            $sort_tag_class = "groupsize";
                            break;
                        }
                    case "distance":
                        {
                            $all_event_order_by = "event_id DESC";
                            $sort_tag_class = "distance";
                            break;
                        }
                }
            }
            $this->view->sort_tag_class = $sort_tag_class;
            $all_event_records = $custom_model->getAllEventDetails($all_event_where_cond, $all_event_order_by, NULL, true)->limitPage($page, 8)->query()->fetchAll();

            if ($sort_tag_class == "date")
            {
                foreach ($all_event_records as $new_key => $new_value)
                {
                    $getEventDate = strtotime(getEventDate($new_value["event_date"]));
                    $all_event_records[$new_key]["event_new_date_to_sort"] = $getEventDate;
                }

                sksort($all_event_records, "event_new_date_to_sort", TRUE);
            }
            else if ($sort_tag_class == "distance")
            {
//                foreach ($all_event_records as $new_key => $new_value)
//                {
//                    $event_address = stripslashes(trim($new_value["event_address"] . ", " . $new_value["event_location"]));
//                    $event_distance = getCurrentEventDistance($event_address, NULL, "M", FALSE);
//                    $all_event_records[$new_key]["event_distance_to_sort"] = $event_distance;
//                }
////            prd($all_event_records);
//
//                sksort($all_event_records, "event_distance_to_sort", TRUE);
            }

            $celebrity_records = $model->getAllRecordDetail(TABLE_EVENTS, "event_celebrity_event = '1' AND event_active='1'", "rand()", "4");
            $sponsored_records = $custom_model->getAllEventDetails("event_active = '1'", "rand()", "4");

            $this->view->allEvents = $all_event_records;
            $this->view->celebrityEvents = $celebrity_records;
            $this->view->sponsoredEvents = $sponsored_records;


            //pagination coding
            $all_event_records_paging = $custom_model->getAllEventDetails($all_event_where_cond, $all_event_order_by, NULL, true);

            $adapter = new Zend_Paginator_Adapter_DbSelect($all_event_records_paging);

            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(8);
            $paginator->setCurrentPageNumber($page);
            $this->view->paginator = $paginator;
        }

        public function logoutAction()
        {
            $auth = Zend_Auth::getInstance();
            $auth->clearIdentity();
            $x = new Zend_Auth_Storage_Session('User_Auth');
            $x->clear();

            $cookie_value = trim($_COOKIE["front_cookie"]);
            unset($_COOKIE["front_cookie"]);
            $model = new Application_Model_Common();
            $whereCond = "cookie_value = '$cookie_value'";
            $model->deleteRecord($whereCond, TABLE_COOKIES);

            $this->_redirect('');
        }

        public function activateaccountAction()
        {
            $activation_link = $this->getParam("activation_link");
            if (!empty($activation_link))
            {
                $model = new Application_Model_Common();
                $is_exists = $model->is_exists(TABLE_USERS, "user_activation_link='$activation_link'");
                if (empty($is_exists))
                {
                    $_SESSION["messageError"] = "No such record found.";
                    $this->_redirect("");
                }
                else
                {
                    $user_status = $is_exists["user_active"];
                    if ($user_status == '1')
                    {
                        $_SESSION["messageError"] = "Your account is already active.";
                        $this->_redirect("");
                    }
                    else
                    {
                        $model->updateRecordData(array("user_active" => "1", "user_activation_link" => ""), "user_activation_link='$activation_link'", TABLE_USERS);
                        $_SESSION["messageSuccess"] = "Your account is now active.";
                        $this->_redirect("");
                    }
                }
            }
        }

    }

    